package com.yixun.lbx;

import com.yixun.lbx.service.LbxRedisService;
import com.yixun.lbx.service.LegalInfoServie;
import com.yixun.util.LogUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.thymeleaf.util.LoggingUtils;

import javax.annotation.Resource;
import java.util.Set;
import java.util.logging.Logger;

@Component
public class StartPingService implements CommandLineRunner {
    private final LbxRedisService redisService;
    private final LegalInfoServie legalInfoServie;

    public StartPingService(LbxRedisService redisService, LegalInfoServie legalInfoServie) {
        this.redisService = redisService;
        this.legalInfoServie = legalInfoServie;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("服务器启动完成，开始检索定时任务...");
        startTimer();
    }
    /**
     * @apiNote  startTimer 查询redis数据库中书否有等待执行的定时任务 如果有就启动
     */
    private void startTimer() {
        Set<String> timers;
        try {
            timers = redisService.getTimers("timer:");
            if (timers.isEmpty()) {
            }
            for (String timer : timers) {
                String[] strings = timer.split(":");
                legalInfoServie.timer(strings[1]);
            }
        } catch (Exception e) {
            System.out.println("警告：Redis连接异常，可能影响部分功能！"+e.getMessage());
        }

    }
}